﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;

namespace DAO
{
    public class SanPhamDAO
    {
        ConnectDB conn = new ConnectDB();
        string str;
        //Load dữ liệu
        public DataTable LoadSanPham()
        {
            return conn.LayDuLieu("select *from SanPham");
        }
        // isert va update
        public void IsUp_SP(SanPhamDTO p, bool flag)
        {
            string masp = p.MaSP;
            string makho = p.MaKho;
            string maquay = p.MaQuay;
            string tensp = p.TenSP;
            string nhacc = p.NhaCC;
            float dongia = p.DonGia;
            float giaban = p.GiaBan;
            float giaKM = p.GiaKhuyenMai;
            if (flag == true)
            {
                str = "INSERT SanPham (MaKho, MaQuay, TenSP, NhaCC, DonGia, GiaBan, GiaKhuyenMai) VALUES('" + makho + "','" + maquay + "',N'" + tensp + "',N'" + nhacc + "'," + dongia + "," + giaban + "," + giaKM + ")";
            }
            else
            {
                str =  "UPDATE SanPham SET MaKho ='" + makho + "', MaQuay ='" + maquay + "', TenSP =N'" + tensp + "', NhaCC =N'" + nhacc + "', DonGia =" + dongia + ", GiaBan =" + giaban + ", GiaKhuyenMai=" + giaKM + " WHERE MaSP='" + masp + "'";

            }
            conn.excute(str );
        }
        //del
        public void Del_SP(SanPhamDTO p)
        {
            string MaSP = p.MaSP;
            str = "DELETE SanPham WHERE MaSP='" + MaSP + "'";
            conn.excute(str);
        }
        //tim san pham
        public DataTable Tim_SP(SanPhamDTO p, bool flag)
        {
            string MaSP = p.MaSP ;
            string tensp = p.TenSP ;
            if (flag == true)
            {
                str = "select * from SanPham where MaSP like'%" + MaSP + "%'";
            }
            else
            {
                str = "select * from SanPham where TenSP Like '%" + tensp + "%'";
            }
            return conn.LayDuLieu(str);
        }
        //danh sach kho
       public DataTable Kho()
       {
           return conn.LayDuLieu("select MaKho from Kho");
       }
        //quay
       public DataTable Quay()
       {
           return conn.LayDuLieu("select MaQuay from Quay");
       }
    }
}
